﻿//
// (C) Copyright 2009 Irantha Suwandarathna (irantha@gmail.com)
// All rights reserved.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading;
using EffiProz.Core;
using EffiProz.Core.Lib;
using EffiProz.Core.Persist;


namespace EffiProz.Test
{
    public abstract class BaseTest
    {
        protected SessionInterface sessionProxy;
        protected String user = "sa";
        protected String password = "";
        protected String connType = DatabaseURL.S_MEM;
        protected String host = "localhost";
        protected int port = 0;
        protected String path = "Test";
        protected String database = "Test";
        protected string sql = "";
        protected EffiProzProperties props = new EffiProzProperties();
        protected string dataPath = "../../../EffiProzTest/Data/";


        static int databaseCounter = 0;

        public static string GetNewDatabaseName()
        {
            lock (typeof(BaseTest))
            {
                return "DB" + databaseCounter++;
            }
        }

        protected void CleanUp(string prefix)
        {
            return;
            //string[] files = Directory.GetFiles(dataPath, prefix + "*");
            //foreach (var file in files)
            //    File.Delete(file);

            // do
            //{
            //    Thread.Sleep(200);
            //    files = Directory.GetFiles(dataPath, prefix + "*");
            //}
            //while (files.Length > 0);
        }


        public BaseTest()
        {
            sessionProxy = DatabaseManager.newSession(connType, database,
                      user, password, props, "", 0);          
        }

        public SessionInterface ConnectToMemoryDB()
        { 
            string dbName = GetNewDatabaseName();
            SessionInterface sessionProxy = DatabaseManager.newSession(connType, dbName,
                      user, password, props, "", 0);
            return sessionProxy;
        }

        public SessionInterface ConnectToMemoryDB(EffiProzProperties props)
        {        
            string dbName = GetNewDatabaseName();
            SessionInterface sessionProxy = DatabaseManager.newSession(connType, dbName,
                      user, password, props, "", 0);
            return sessionProxy;
        }

        public SessionInterface ConnectToFileDB(String dbName)
        { 
            SessionInterface sessionProxy = DatabaseManager.newSession(DatabaseURL.S_FILE, dataPath + dbName+"\\Test",
                      user, password, props, "", 0);
            return sessionProxy;
        }

        public SessionInterface ConnectToFileDB(String dbName, EffiProzProperties props)
        {
            SessionInterface sessionProxy = DatabaseManager.newSession(DatabaseURL.S_FILE, dataPath + dbName + "\\Test",
                      user, password, props, "", 0);
            return sessionProxy;
        }


    }
}
